package com.atguigu.gmall.list.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消费消息
 */
@Component
@Slf4j
public class ListReceiver {
    @Autowired
    private SearchService searchService;

    /**
     * 监听商品上架消费者
     * @param skuId
     * @param channel
     * @param message
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(MqConst.EXCHANGE_DIRECT_GOODS),
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true"),
            key = MqConst.ROUTING_GOODS_UPPER
    ))

    public void processGoodsUpper(Long skuId, Channel channel, Message message){
        if(skuId != null){
            log.info("[搜索服务]监听商品上架,商品id:{}",skuId);
            searchService.upperGoods(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 监听商品下架消费者
     * @param skuId
     * @param channel
     * @param message
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(MqConst.EXCHANGE_DIRECT_GOODS),
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true"),
            key = MqConst.ROUTING_GOODS_UPPER
    ))

    public void processGoodsLower(Long skuId, Channel channel, Message message){
        if(skuId != null){
            log.info("[搜索服务]监听商品下架,商品id:{}",skuId);
            searchService.lowerGoods(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
